<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Web
 * @version $Id$
 */

/**	
 * The Request Handler Resolver
 *	
 * @package auxo
 * @subpackage Web
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class tx_auxo_RequestHandlerResolver  {

	/**
	 * Hash array of possible request handler
	 *
	 * @var unknown_type
	 */
	protected $handlers = NULL;
	protected $defaultHandler = NULL;
	
	public function setDefaultHandler($default) {
		$this->defaultHandler = $default;	
	}
	
	public function setHandlers(tx_auxo_HashArray $handlers) {
		$this->handlers = $handlers;
	}
	
	public function resolve() {
		foreach($this->handlers as $handler) {
			if ($handler->canHandle()) {
				$possibleHandlers[$handler->getPriority()] = $handler;
			}
		}
		
		if (! isset($possibleHandlers)) {
			if (! $this->defaultHandler) {
				return NULL;
			}
			return $this->defaultHandler;
		}
		
		ksort($possibleHandlers);
		return array_slice($possibleHandlers, 0, 1);		
	}
}
?>